home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / text / manipulation / snap164.lha / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  3.7 KB  |  169 lines

  1. #if __SASC
  2. #include "snap.h"
  3. #endif
  4.  
  5. /* Auto: make "CCEXTRA=-wq -qf"
  6. */
  7.  
  8. #define SCREENTOP\
  9.    (screen->TopEdge << ((screen->ViewPort.Modes & LACE)? 0: 1))
  10.  
  11. IMPORT struct SnapRsrc *SnapRsrc;
  12. IMPORT struct IntuitionBase *IntuitionBase;
  13. #if 0
  14. IMPORT struct DiskFontBase *DiskfontBase;
  15. #endif
  16. IMPORT struct Library *    DiskfontBase;
  17.  
  18. struct Screen *WhichScreen()
  19. {
  20.     REGISTER struct Screen *screen;
  21.     REGISTER ULONG lock;
  22.  
  23.     lock = LockIBase(0);
  24.     screen = IntuitionBase->FirstScreen;
  25.     while (screen && IntuitionBase->MouseY < SCREENTOP) {
  26.         screen = screen->NextScreen;
  27.     }
  28.     if (screen == NULL) {     /* Shouldn't happen */
  29.         screen = IntuitionBase->ActiveScreen;
  30.     }
  31.     UnlockIBase(lock);
  32.     return screen;
  33. }
  34.  
  35. struct Window *WhichWindow(screen)
  36. struct Screen *screen;
  37. {
  38.     struct Layer *layer;
  39.     layer = (struct Layer *)WhichLayer(&screen->LayerInfo,
  40.       (LONG)screen->MouseX, (LONG)screen->MouseY);
  41.     if (layer) {
  42.         return (struct Window *)layer->Window;
  43.     } else {
  44.         return NULL;
  45.     }
  46. }
  47.  
  48. VOID FreePlanes(bm, width, height)
  49. struct BitMap *bm;
  50. LONG width, height;
  51. {
  52.     SHORT i;
  53.     for (i=0; i < bm->Depth; i++) {
  54.         if (bm->Planes[i]) {
  55.             FreeRaster(bm->Planes[i], width, height);
  56.         }
  57.     }
  58. }
  59.  
  60. WORD AllocPlanes(bm, width, height)
  61. struct BitMap *bm;
  62. LONG width, height;
  63. {
  64.     WORD i;
  65.     for (i=0; i < bm->Depth; i++) {
  66.         bm->Planes[i] = NULL;
  67.     }
  68.     for (i=0; i < bm->Depth; i++) {
  69.         if (!(bm->Planes[i] = AllocRaster(width, height))) {
  70.             return 0;
  71.         }
  72.         BltClear((char *)bm->Planes[i], RASSIZE(width, height), NULL);
  73.     }
  74.     return 1;
  75. }
  76.  
  77. VOID CacheWindow(struct Window *Win,
  78.                  LONG xoff, LONG yoff,
  79.                  SHORT fw, SHORT fh,
  80.                  struct TextFont *font)
  81. {
  82.     struct CacheWindow *cw;
  83.  
  84.     if (!(cw = Create(CacheWindow))) {
  85.         return;
  86.     }
  87.     cw->Window = Win;
  88.     cw->xoff = xoff;
  89.     cw->yoff = yoff;
  90.     cw->fw = fw;
  91.     cw->fh = fh;
  92.     cw->font = font;
  93.     AddHead((struct List *)&SnapRsrc->CachedWindows, (struct Node *)cw);
  94.     if (SnapRsrc->CacheSize > 0) {
  95.         --SnapRsrc->CacheSize;
  96.     } else {
  97.         cw = (struct CacheWindow *)RemTail((struct List *)&SnapRsrc->CachedWindows);
  98.         Kill(cw);
  99.     }
  100. }
  101.  
  102. VOID CacheSync(Font)
  103. struct TextFont *Font;
  104. {
  105.     struct CacheWindow *cw;
  106.     struct CacheWindow *safe;
  107.  
  108.     SAFEMAPLIST(&SnapRsrc->CachedWindows, struct CacheWindow *, cw, safe) {
  109.         if (cw->font == SnapRsrc->AlternateFont) {
  110.             Remove((struct Node *)cw);
  111.             Kill(cw);
  112.         }
  113.     }
  114. }
  115.  
  116. struct CacheWindow *GetCachedWindow(Screen, Win)
  117. struct Screen *Screen;
  118. struct Window *Win;
  119. {
  120.     struct CacheWindow *cw;
  121.  
  122.     MAPLIST(&SnapRsrc->CachedWindows, struct CacheWindow *, cw) {
  123.         if (cw->Window == Win) {
  124.             LONG LockVal = LockIBase(0L);
  125.             REGISTER struct Window *w = Screen->FirstWindow;
  126.             while (w && w != Win) {
  127.                 w = w->NextWindow;
  128.             }
  129.             UnlockIBase(LockVal);
  130.             if (!w) {
  131.                 return NULL;
  132.             }
  133.             Remove((struct Node *)cw);
  134.             AddHead((struct List *)&SnapRsrc->CachedWindows, (struct Node *)cw);
  135.             return cw;
  136.         }
  137.     }
  138.     return NULL;
  139. }
  140.  
  141. struct TextFont *SmartOpenFont(ta)
  142. struct TextAttr *ta;
  143. {
  144.     struct TextFont *MemF;
  145.     struct TextFont *DskF = NULL;
  146.  
  147.     if ((MemF = OpenFont(ta)) && ta->ta_YSize == MemF->tf_YSize) {
  148.         return MemF;
  149.     }
  150.  
  151.     if (DiskfontBase) {
  152.         DskF = OpenDiskFont(ta);
  153.     }
  154.  
  155.     if (!MemF) {
  156.         return DskF;
  157.     }
  158.     if (!DskF) {
  159.         return MemF;
  160.     }
  161.     if (abs(ta->ta_YSize - MemF->tf_YSize) <=
  162.         abs(ta->ta_YSize - DskF->tf_YSize)) {
  163.         CloseFont(DskF);
  164.         return MemF;
  165.     }
  166.     CloseFont(MemF);
  167.     return DskF;
  168. }
  169.